//
//  ByMineInfoVC.m
//  hutouben_ios
//
//  Created by justiOS on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByMineInfoVC.h"
// vc
#import "ByChangeNicknameViewController.h"   // 修改昵称
#import "ByDriverAuthenticationVC.h"         // 司机认证
#import "ByAuthenticationInfoVC.h"           // 认证信息
@interface ByMineInfoVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIImage *icon;
@property (nonatomic, strong) NSArray *subjects;
@property (nonatomic, strong) NSMutableArray *subjectDetails;
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource;
@end

@implementation ByMineInfoVC
#pragma mark - getter

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        _iconView.image = [UIImage imageWithColor:HTB_THEME_COLOR];
        _iconView.layer.cornerRadius = _iconView.height / 2;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}
- (NSArray *)subjects {
    if (!_subjects) {
        _subjects = @[@[@"头像", @"昵称", @"认证信息"]];
    }
    return _subjects;
}
- (NSMutableArray *)subjectDetails {
    if (!_subjectDetails) {
        NSArray *temp = @[@[@"", @"未设置", @"未认证"]];
        _subjectDetails = [[NSMutableArray alloc] initWithArray:temp];
    }
    return _subjectDetails;
}
- (ByIconChooseDataSource *)iconChooseDataSource {
    if (!_iconChooseDataSource) {
        _iconChooseDataSource = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource fireWithViewController:self];
    }
    return _iconChooseDataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addLeftBackBarButtonItem];
    
    [self createTableView];

}


#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    self.table.backgroundColor = HTB_BG_WHITE;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
}


#pragma mark - UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.subjects.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *temp = self.subjects[section];
    return temp.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = FONT(14);
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.font = FONT(14);
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.accessoryView = self.iconView;
        }
    }
    
    
    NSArray *subjectTemp = self.subjects[indexPath.section];
    NSArray *detailSubjectTemp = self.subjectDetails[indexPath.section];
    
    cell.detailTextLabel.text = detailSubjectTemp[indexPath.row];
    cell.textLabel.text = subjectTemp[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 50;
    }
    return 40;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    } else {
        return 4;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 4;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section == 0) {
        /** 选择头像 */
        if (indexPath.row == 0) {
            
            [self.iconChooseDataSource makeConfigToSelectIcon];
            __weak typeof(self) weakself = self;
            [self.iconChooseDataSource setCompleteBlock:^(UIImage *image) {
                
                [weakself uploadIcon:image];
            }];
        }
        //修改昵称
        if (indexPath.row == 1) {
            ByChangeNicknameViewController *vc = [[ByChangeNicknameViewController alloc] init];
            [self by_normalPush:vc hasNavi:YES];
        }
        
        if (indexPath.row == 2) {
            // 判断是否认证过
            if (indexPath.row == 1) {
                // 未认证
                ByDriverAuthenticationVC *vc = [[ByDriverAuthenticationVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];

            }else{
                // 已认证
                ByAuthenticationInfoVC *vc = [[ByAuthenticationInfoVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            }
        }
        
    }
}

#pragma mark - net
- (void)uploadIcon:(UIImage *)icon {
    self.icon = icon;
        // 上传头像
    
}

#pragma mark - 辅助方法
- (void)updateUserInfo {
    
    
    NSMutableArray *ary1 = [NSMutableArray arrayWithCapacity:5];

    NSArray *temp1 = @[@"", @"未登录", @"未登录"];

    [ary1 addObjectsFromArray:temp1];

    /** 刷新头像 */
//    [self.iconView setImageWithURL:[NSURL URLWithString:_user.head] placeholderImage:DEFAULT_HEAD];
    
    //昵称
//    [ary1 replaceObjectAtIndex:1 withObject:_user.nickname];
    //性别
    
    
    
    
    [self.subjectDetails replaceObjectAtIndex:0 withObject:ary1];

    
    [self.table reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
